這一篇來介紹Checkbox元件,我們常在驗證是否為機器人的欄位中看到它。它有兩種狀態:打勾或不打勾,所以我們可以用boolean的變化來存取。
我們使用以下函式:
//CheckBox監聽器
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        
    }
});
當CheckBox有打勾時,boolean值b會變成true,沒有打勾會變成false,就能夠透過b值來觸發想要的功能。
整合起來如下:
MainActivity:
public class MainActivity extends AppCompatActivity {
    //定義變數check,checkBox
    private boolean check;
    CheckBox checkBox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //綁定元件和變數
        checkBox=findViewById(R.id.checkBox);
        //checkBox監聽器
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                
                //將b傳給check,check為true或是false時將觸發相對應的功能
                check=b;
                if(check==true)
                    Toast.makeText(MainActivity.this,"已勾選 '我同意以上條款'", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(MainActivity.this,"請勾選 '我同意以上條款'", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="136dp"
        android:layout_height="48dp"
        android:text="我同意以上條款"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.356" />
</androidx.constraintlayout.widget.ConstraintLayout>
成品:
而CheckBox中也有各種屬性,以下列出常用的幾個:
利用android:onClick="ifOnclick"這行屬性,我們可以以另一種方式達成上面實作的效果。如下:
MainActivity:
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //為點擊時與未點擊的狀態所產生的各自功能
    public void ifOnclick(View view)
    {
        
        //設定變數
        CheckBox checkBox;
        
        //綁定元件跟變數
        checkBox=findViewById(R.id.checkBox);
        
        //調用.isChecked()方法,將值設給變數check
        boolean check = checkBox.isChecked();
        if (check==true)
        {
            Toast.makeText(this, "已勾選 '我同意以上條款'", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(this, "請勾選 '我同意以上條款'", Toast.LENGTH_SHORT).show();
        }
    }
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox"
        android:onClick="ifOnclick"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
跟上面的效果是完全一樣的~~